library(ggthemr)
ggthemr('flat', spacing = 0.5, type = 'inner',layout="scientific")
anes2012$oldnew <- relevel(as.factor(anes2012$oldnew),ref="Old")
anes2012$oldnew2 <-  relevel(as.factor(anes2012$oldnew2),ref="Old")
table(anes2012$oldnew)
a <- (lm(pk~efficacy1pre*wordsumz1*oldnew,anes2012))
summary(a)
summary(lm(pk~efficacy1pre*oldnew,subset(anes2012,wordsumz1>.5)))


b <- (lm(pk~efficacy2pre*wordsumz1*oldnew,anes2012))
c <- (lm(pk~efficacy3pre*wordsumz1*oldnew,anes2012))
d <- (lm(pk~efficacy4pre*wordsumz1*oldnew,anes2012))
e <- (lm(pk~pre_external*wordsumz1*oldnew,anes2012))
f <- (lm(pk~pre_internal*wordsumz1*oldnew,anes2012))

g <- (lm(pk~efficacy1po*wordsumz1*oldnew2,anes2012))
h <- (lm(pk~efficacy2po*wordsumz1*oldnew2,anes2012))
i <- (lm(pk~efficacy3po*wordsumz1*oldnew2,anes2012))
j <- (lm(pk~efficacy4po*wordsumz1*oldnew2,anes2012))
k <- (lm(pk~po_external*wordsumz1*oldnew2,anes2012))
l <- (lm(pk~po_internal*wordsumz1*oldnew2,anes2012))

names(a$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(b$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(c$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(d$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(e$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(f$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(g$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(h$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(i$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(j$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(k$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(l$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )

library(stargazer)
stargazer(a,b,c,d,e,f,g,h,i,j,k,l,omit.stat =c("f","ser"),float.env = "sidewaystable",dep.var.caption = "Efficacy Measure",column.labels =   c("1","2","3","4","External","Internal","1","2","3","4","External","Internal"),model.numbers =  F,no.space = T,dep.var.labels.include = F,title="Predicting newspaper consumption from three-way interactions between question type, efficacy, and verbal ability",label = "tab:wordsumz1_news",out = "Graphs/threewaywordsumz1_pk.tex",initial.zero = T,digits = 2,font.size = "scriptsize",intercept.top = T,intercept.bottom=F)

threeway1 <- data.frame(do.call("rbind",lapply(list(a,b,c,d,e,f,g,h,i,j,k,l),function(x)summary(x)$coefficients[8,1:2])))
threeway1$question <- c("Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index","Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index")
threeway1$wave <- c(rep("Wave 1",6),rep("Wave 2",6))
names(threeway1)[2] <- "se"
panela <- ggplot(threeway1,aes(y=Estimate,x=question))+geom_errorbar(aes(ymax=Estimate+1.64*se,ymin=Estimate-1.64*se),height=0)+geom_point(size=2,colour="black")+facet_grid(~wave)+xlab("Question")+ylab("b coefficient")+ggtitle("Panel A: DV is R's political knowledge")+geom_hline(y=0)




########################################
########################################
a <- (lm(pctchance~efficacy1pre*wordsumz1*oldnew,anes2012))
b <- (lm(pctchance~efficacy2pre*wordsumz1*oldnew,anes2012))
c <- (lm(pctchance~efficacy3pre*wordsumz1*oldnew,anes2012))
summary (lm(pctchance~efficacy3pre*oldnew,subset(anes2012,wordsumz1<.5)))


d <- (lm(pctchance~efficacy4pre*wordsumz1*oldnew,anes2012))
e <- (lm(pctchance~pre_external*wordsumz1*oldnew,anes2012))
f <- (lm(pctchance~pre_internal*wordsumz1*oldnew,anes2012))

g <- (lm(pctchance~efficacy1po*wordsumz1*oldnew2,anes2012))
h <- (lm(pctchance~efficacy2po*wordsumz1*oldnew2,anes2012))
i <- (lm(pctchance~efficacy3po*wordsumz1*oldnew2,anes2012))
j <- (lm(pctchance~efficacy4po*wordsumz1*oldnew2,anes2012))
k <- (lm(pctchance~po_external*wordsumz1*oldnew2,anes2012))
l <- (lm(pctchance~po_internal*wordsumz1*oldnew2,anes2012))
summary(lm(polactivity~efficacy3po*oldnew2,subset(anes2012,as.numeric(wordsumz1)==2)))

names(a$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(b$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(c$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(d$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(e$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(f$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(g$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(h$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(i$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(j$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(k$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(l$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )

library(stargazer)
stargazer(a,b,c,d,e,f,g,h,i,j,k,l,omit.stat =c("f","ser"),float.env = "sidewaystable",dep.var.caption = "Efficacy Measure",column.labels =   c("1","2","3","4","External","Internal","1","2","3","4","External","Internal"),model.numbers = F,no.space = T,dep.var.labels.include = F,title="Predicting percent chance of voting from three-way interactions between question type, efficacy, and verbal ability",label = "tab:wordsumz1_pctchace",out = "Graphs/threewayvotingwordsumz1.tex",initial.zero = T,digits = 2,font.size = "scriptsize",intercept.top = T,intercept.bottom=F)


threeway1 <- data.frame(do.call("rbind",lapply(list(a,b,c,d,e,f,g,h,i,j,k,l),function(x)summary(x)$coefficients[8,1:2])))
threeway1$question <- c("Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index","Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index")
threeway1$wave <- c(rep("Wave 1",6),rep("Wave 2",6))
names(threeway1)[2] <- "se"
panelb <- ggplot(threeway1,aes(y=Estimate,x=question))+geom_errorbar(aes(ymax=Estimate+1.64*se,ymin=Estimate-1.64*se),height=0)+geom_point(size=2,colour="black")+facet_grid(~wave)+xlab("Question")+ylab("b coefficient")+ggtitle("Panel B: DV is R's Percent Chance of Voting")+geom_hline(y=0)

########################################
a <- (lm(polactivity~efficacy1pre*wordsumz1*oldnew,anes2012))
b <- (lm(polactivity~efficacy2pre*wordsumz1*oldnew,anes2012))
c <- (lm(polactivity~efficacy3pre*wordsumz1*oldnew,anes2012))
d <- (lm(polactivity~efficacy4pre*wordsumz1*oldnew,anes2012))
e <- (lm(polactivity~pre_external*wordsumz1*oldnew,anes2012))
f <- (lm(polactivity~pre_internal*wordsumz1*oldnew,anes2012))

g <- (lm(polactivity~efficacy1po*wordsumz1*oldnew2,anes2012))
h <- (lm(polactivity~efficacy2po*wordsumz1*oldnew2,anes2012))
i <- (lm(polactivity~efficacy3po*wordsumz1*oldnew2,anes2012))
j <- (lm(polactivity~efficacy4po*wordsumz1*oldnew2,anes2012))
k <- (lm(polactivity~po_external*wordsumz1*oldnew2,anes2012))
l <- (lm(polactivity~po_internal*wordsumz1*oldnew2,anes2012))



names(a$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(b$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(c$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(d$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(e$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(f$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(g$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(h$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(i$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(j$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(k$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )
names(l$coefficients) <- c("Intercept","Efficacy","Verbal Ability","Form","Effic. x Verbal Ability","Effic. x Form","Verbal Ability x Form","Effic. x Verbal Ability x Form" )

library(stargazer)
stargazer(a,b,c,d,e,f,g,h,i,j,k,l,omit.stat =c("f","ser"),float.env = "sidewaystable",dep.var.caption = "Efficacy Measure",column.labels =   c("1","2","3","4","External","Internal","1","2","3","4","External","Internal"),model.numbers = F,no.space = T,dep.var.labels.include = F,title="Predicting political activism from three-way interactions between question type, efficacy, and verbal ability",label = "tab:wordsumz1_activism",out = "Graphs/threewaywordsumz1_activism.tex",initial.zero = T,digits = 2,font.size = "scriptsize",intercept.top = T,intercept.bottom=F)

threeway1 <- data.frame(do.call("rbind",lapply(list(a,b,c,d,e,f,g,h,i,j,k,l),function(x)summary(x)$coefficients[8,1:2])))
threeway1$question <- c("Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index","Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index")
threeway1$wave <- c(rep("Wave 1",6),rep("Wave 2",6))
names(threeway1)[2] <- "se"
panelc <- ggplot(threeway1,aes(y=Estimate,x=question))+geom_errorbar(aes(ymax=Estimate+1.64*se,ymin=Estimate-1.64*se),height=0)+geom_point(size=2,colour="black")+facet_grid(~wave)+xlab("Question")+ylab("b coefficient")+ggtitle("Panel C: DV is R's political activity")+geom_hline(y=0)


pdf("threewayplot_wordsumz1.pdf",width=12,height=12)
multiplot(panela,panelb,panelc)
dev.off()

